{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Methods and Members\n",
        "\n",
        "Watch the full [C# 101 video](https://www.youtube.com/watch?v=xLhm3bEG__c&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=17) for this module.\n",
        "\n",
        "Here's your bank account so far! It doesn't do much right now, only prints out the owner and balance. It doesn't even have an account number yet. You'll work on a transaction class, which has been added as an empty class for you.\n",
        "\n",
        "> Run each code chunk below and see what gets printed. This is what you ended up with last time."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "public class BankAccount\n",
        "{\n",
        "    // Properties\n",
        "    public string Number { get; }\n",
        "    public string Owner { get; set; }\n",
        "    public decimal Balance { get; }\n",
        "\n",
        "    // Constructor\n",
        "    public BankAccount(string name, decimal initialBalance)\n",
        "    {\n",
        "        this.Owner = name;\n",
        "        this.Balance = initialBalance;\n",
        "    }\n",
        "\n",
        "    // Functions\n",
        "    public void MakeDeposit(decimal amount, DateTime date, string note)\n",
        "    {\n",
        "    }\n",
        "\n",
        "    public void MakeWithdrawal(decimal amount, DateTime date, string note)\n",
        "    {\n",
        "    }\n",
        "}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Account was created for Kendra with 1000 dollars\r\n"
          ]
        }
      ],
      "source": [
        "var account = new BankAccount(\"Kendra\", 1000);\n",
        "Console.WriteLine($\"Account{account.Number} was created for {account.Owner} with {account.Balance} dollars\");"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## #1: Account Number\n",
        "\n",
        "You need a starting number, that you can base the new account numbers off of, to insure that all the accounts are unique. Below is the code for this number \"seed\". What does it mean?\n",
        "\n",
        "- **Private**: This means that no client can see this number. It's internal, part of the internal workings of the code.\n",
        "- **Static**: This mean the number is universal amongst all individual accounts. If one account changes it, then that number is updated for all the other accounts. This is how you can make it a great way to make sure the account numbers are all unique! Once an bank account uses it for it's bank number, it can add one to the account seed, and the next new bank account has a new number.\n",
        "\n",
        "> Copy the code below and paste it in the `// Properties` section of the `BankAccount` class.\n",
        "\n",
        "```csharp\n",
        "private static int accountNumberSeed = 1234567890;\n",
        "```\n",
        "\n",
        "> Copy this next bit of code and add it to the constructor.\n",
        "\n",
        "```csharp\n",
        "this.Number = accountNumberSeed.ToString();\n",
        "accountNumberSeed++;\n",
        "```\n",
        "\n",
        "> Run this code and see what happens!"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "public class BankAccount\n",
        "{\n",
        "    // Properties\n",
        "    public string Number { get; }\n",
        "    public string Owner { get; set; }\n",
        "    public decimal Balance { get; }\n",
        "    //(Paste first bit here!)\n",
        "\n",
        "    // Constructor\n",
        "    public BankAccount(string name, decimal initialBalance)\n",
        "    {\n",
        "        this.Owner = name;\n",
        "        this.Balance = initialBalance;\n",
        "        //(Paste second part here!)\n",
        "\n",
        "    }\n",
        "\n",
        "    // Functions\n",
        "    public void MakeDeposit(decimal amount, DateTime date, string note)\n",
        "    {\n",
        "    }\n",
        "\n",
        "    public void MakeWithdrawal(decimal amount, DateTime date, string note)\n",
        "    {\n",
        "    }\n",
        "}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Account  was created for Kendra with 1000 dollars\r\n"
          ]
        }
      ],
      "source": [
        "var account = new BankAccount(\"Kendra\", 1000);\n",
        "Console.WriteLine($\"Account {account.Number} was created for {account.Owner} with {account.Balance} dollars\");"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## #2: Transaction Properties\n",
        "\n",
        "The next part you need is a balance! One way you could do this is just keep a running tab. However, another way to do it is to create a history of transactions. To do that, you're going to make a little transaction class, that records one transaction.\n",
        "\n",
        "> paste the properties below into the class `Transaction`\n",
        "\n",
        "```csharp\n",
        "public decimal Amount { get; }\n",
        "public DateTime Date { get; }\n",
        "public string Notes { get; }\n",
        "```"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "public class Transaction\n",
        "{\n",
        "    // Properties (Paste here!)\n",
        "\n",
        "    // Constructor\n",
        "}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## #3: Transaction Constructor\n",
        "\n",
        "Next, you need to add the constructor to the class!\n",
        "\n",
        "> Add the following code to the `Transaction` class, under constructor.\n",
        "\n",
        "```csharp\n",
        "public Transaction(decimal amount, DateTime date, string note)\n",
        "{\n",
        "    this.Amount = amount;\n",
        "    this.Date = date;\n",
        "    this.Notes = note;\n",
        "}\n",
        "```"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "public class Transaction\n",
        "{\n",
        "    // Properties\n",
        "    public decimal Amount { get; }\n",
        "    public DateTime Date { get; }\n",
        "    public string Notes { get; }\n",
        "\n",
        "    // Constructor (Paste here!)\n",
        "}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## #4: Update BankAccount to match\n",
        "\n",
        "Now that you have a transaction class, you can use that in our bank account. First, you need to make a list of transactions.\n",
        "\n",
        "> Copy the following code into the Properties section.\n",
        "\n",
        "```csharp\n",
        "private List<Transaction> allTransactions = new List<Transaction>();\n",
        "```"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "using System.Collections.Generic;\n",
        "\n",
        "public class BankAccount\n",
        "{\n",
        "    // Properties\n",
        "    public string Number { get; }\n",
        "    public string Owner { get; set; }\n",
        "    public decimal Balance{ get;}\n",
        "    private static int accountNumberSeed = 1234567890;\n",
        "    //(Paste here!)\n",
        "\n",
        "    // Constructor\n",
        "    public BankAccount(string name, decimal initialBalance)\n",
        "    {\n",
        "        this.Owner = name;\n",
        "        this.Balance = initialBalance;\n",
        "        this.Number = accountNumberSeed.ToString();\n",
        "        accountNumberSeed++;\n",
        "\n",
        "    }\n",
        "\n",
        "    // Functions\n",
        "    public void MakeDeposit(decimal amount, DateTime date, string note)\n",
        "    {\n",
        "    }\n",
        "\n",
        "    public void MakeWithdrawal(decimal amount, DateTime date, string note)\n",
        "    {\n",
        "    }\n",
        "}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## #5: Updating Balance\n",
        "\n",
        "Now that you have a list of transactions that you can use, you need to attach `Balance` to that. What you want to do is, whenever someone wants to get balance, the code checks the list of transactions and tallies it all up, before returning the answer. You can do this by attaching some instructions to the `get` in Balance!\n",
        "\n",
        "> In `BankAccount`, replace `public decimal Balance { get;}` with the following code:\n",
        "\n",
        "```csharp\n",
        "public decimal Balance\n",
        "{\n",
        "    get\n",
        "    {\n",
        "        decimal balance = 0;\n",
        "        foreach (var item in allTransactions)\n",
        "        {\n",
        "            balance += item.Amount;\n",
        "        }\n",
        "\n",
        "        return balance;\n",
        "    }\n",
        "}\n",
        "```"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "public class BankAccount\n",
        "{\n",
        "    // Properties\n",
        "    public string Number { get; }\n",
        "    public string Owner { get; set; }\n",
        "    public decimal Balance { get; }  // replace this line!\n",
        "    private static int accountNumberSeed = 1234567890;\n",
        "    private List<Transaction> allTransactions = new List<Transaction>();\n",
        "\n",
        "    // Constructor\n",
        "    public BankAccount(string name, decimal initialBalance)\n",
        "    {\n",
        "        this.Owner = name;\n",
        "        this.Balance = initialBalance;\n",
        "        this.Number = accountNumberSeed.ToString();\n",
        "        accountNumberSeed++;\n",
        "    }\n",
        "\n",
        "    // Functions\n",
        "    public void MakeDeposit(decimal amount, DateTime date, string note)\n",
        "    {\n",
        "    }\n",
        "\n",
        "    public void MakeWithdrawal(decimal amount, DateTime date, string note)\n",
        "    {\n",
        "    }\n",
        "}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## #6: Fixing errors\n",
        "\n",
        "You may have noticed a red squiggly line under `this.Balance`. There's a new error you created! Because whenever you're getting Balance, it goes through a process of summing up the list of transactions, you can't just say that `Balance` is initial balance. You won't fix this entirely in this module, but you can make the code usable for now.\n",
        "\n",
        "> Remove the line `this.Balance = initialBalance`."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [
        {
          "ename": "Error",
          "evalue": "(27,9): error CS0200: Property or indexer 'BankAccount.Balance' cannot be assigned to -- it is read only",
          "output_type": "error",
          "traceback": [
            "(27,9): error CS0200: Property or indexer 'BankAccount.Balance' cannot be assigned to -- it is read only"
          ]
        }
      ],
      "source": [
        "public class BankAccount\n",
        "{\n",
        "    // Properties\n",
        "    public string Number { get; }\n",
        "    public string Owner { get; set; }\n",
        "    public decimal Balance\n",
        "    {\n",
        "        get\n",
        "        {\n",
        "            decimal balance = 0;\n",
        "            foreach (var item in allTransactions)\n",
        "            {\n",
        "                balance += item.Amount;\n",
        "            }\n",
        "\n",
        "            return balance;\n",
        "        }\n",
        "    }\n",
        "    private static int accountNumberSeed = 1234567890;\n",
        "    private List<Transaction> allTransactions = new List<Transaction>();\n",
        "\n",
        "\n",
        "    // Constructor\n",
        "    public BankAccount(string name, decimal initialBalance)\n",
        "    {\n",
        "        this.Owner = name;\n",
        "        this.Balance = initialBalance; //delete this line\n",
        "        this.Number = accountNumberSeed.ToString();\n",
        "        accountNumberSeed++;\n",
        "    }\n",
        "\n",
        "    // Functions\n",
        "    public void MakeDeposit(decimal amount, DateTime date, string note)\n",
        "    {\n",
        "    }\n",
        "\n",
        "    public void MakeWithdrawal(decimal amount, DateTime date, string note)\n",
        "    {\n",
        "    }\n",
        "}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Review: Where did Initial Balance go?\n",
        "\n",
        "Here's our final code for this module below. There's a problem though! You no longer have an initial balance and have 0 money! Since you tied up your balance with transactions, you're gonna need to be able to make deposits and withdrawals to put money in the bank. You'll learn that in the next module!\n",
        "\n",
        "> Run the code cells below.\n",
        ">\n",
        "> Try making your own transaction methods before the next module! Where are you getting stuck? What do you need to learn?"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "public class Transaction\n",
        "{\n",
        "    // Properties (#2)\n",
        "    public decimal Amount { get; }\n",
        "    public DateTime Date { get; }\n",
        "    public string Notes\n",
        "    {\n",
        "        get;\n",
        "\n",
        "    }\n",
        "\n",
        "    // Constructor (#3)\n",
        "    public Transaction(decimal amount, DateTime date, string note)\n",
        "    {\n",
        "        this.Amount = amount;\n",
        "        this.Date = date;\n",
        "        this.Notes = note;\n",
        "    }\n",
        "}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [],
      "source": [
        "using System.Collections.Generic;\n",
        "\n",
        "public class BankAccount\n",
        "{\n",
        "    // Properties\n",
        "    public string Number { get; }\n",
        "    public string Owner { get; set; }\n",
        "    public decimal Balance  //(#5)\n",
        "    {\n",
        "        get\n",
        "\n",
        "        {\n",
        "            decimal balance = 0;\n",
        "            foreach (var item in allTransactions)\n",
        "            {\n",
        "                balance += item.Amount;\n",
        "            }\n",
        "\n",
        "            return balance;\n",
        "        }\n",
        "\n",
        "\n",
        "    }\n",
        "    private static int accountNumberSeed = 1234567890; //(#1)\n",
        "    private List<Transaction> allTransactions = new List<Transaction>(); //(#4)\n",
        "\n",
        "\n",
        "    // Constructor\n",
        "    public BankAccount(string name, decimal initialBalance)\n",
        "    {\n",
        "        this.Owner = name;\n",
        "        //(#6: deleted \"this.Balance = initialBalance;\")\n",
        "        this.Number = accountNumberSeed.ToString(); //(#1)\n",
        "        accountNumberSeed++; //(#1)\n",
        "\n",
        "    }\n",
        "\n",
        "    // Functions\n",
        "    public void MakeDeposit(decimal amount, DateTime date, string note)\n",
        "    {\n",
        "    }\n",
        "\n",
        "    public void MakeWithdrawal(decimal amount, DateTime date, string note)\n",
        "    {\n",
        "    }\n",
        "}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Account 1234567890 was created for Kendra with 0 dollars\r\n"
          ]
        }
      ],
      "source": [
        "var account = new BankAccount(\"Kendra\", 1000);\n",
        "Console.WriteLine($\"Account {account.Number} was created for {account.Owner} with {account.Balance} dollars\");"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Continue learning\n",
        "\n",
        "There are plenty more resources out there to learn!\n",
        "> [⏩ Next Module - Methods and Exceptions](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/15-Methods%20and%20Exceptions.ipynb)\n",
        ">\n",
        "> [⏪ Last Module - Objects and Classes](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/13-Objects%20and%20Classes.ipynb)\n",
        ">\n",
        "> [Watch the video](https://www.youtube.com/watch?v=xLhm3bEG__c&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=17)\n",
        ">\n",
        "> [Documentation: Object Oriented Coding in C#](https://docs.microsoft.com/dotnet/csharp/fundamentals/tutorials/classes?WT.mc_id=Educationalcsharp-c9-scottha)\n",
        ">\n",
        "> [Start at the beginning: What is C#?](https://www.youtube.com/watch?v=BM4CHBmAPh4&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Other resources\n",
        "\n",
        "Here's some more places to explore:\n",
        "> [Other 101 Videos](https://dotnet.microsoft.com/learn/videos?WT.mc_id=csharpnotebook-35129-website)\n",
        ">\n",
        "> [Microsoft Learn](https://docs.microsoft.com/learn/dotnet/?WT.mc_id=csharpnotebook-35129-website)\n",
        ">\n",
        "> [C# Documentation](https://docs.microsoft.com/dotnet/csharp/?WT.mc_id=csharpnotebook-35129-website)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": ".NET (C#)",
      "language": "C#",
      "name": ".net-csharp"
    },
    "language_info": {
      "file_extension": ".cs",
      "mimetype": "text/x-csharp",
      "name": "C#",
      "pygments_lexer": "csharp",
      "version": "8.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}
